package b2.widget.fort
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.*;
	import b2.widget.*;
	import b2.widget.login.*;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	
	public class MatchWinBox extends Sprite
	{
		private const RANK_NAME:Array = ["st", "nd", "rd", "th"];
		private var oilAward:int;
		private var honourAward:int;
		
		public function MatchWinBox(matchIndex:int, weeklyRanking:int, totalRanking:int)
		{
			super();
			var matchConfig:MatchConfig = Config.matchConfigs[matchIndex];
			var MessageBoxGgr:Class = ResourceHelper.getInstance().getClassByName("MessageBox0");
			var mainSprite:Sprite = new MessageBoxGgr();
			addChild(mainSprite);

			var message:String = "恭喜您获得了";
			if (weeklyRanking >= 0)
				message += "周排行第" + (weeklyRanking + 1).toString() + "名";
			if (totalRanking >= 0)
				message += "，总排行第" + (totalRanking + 1).toString() + "名";
			message += "！";
			//信息文字
			var messageText:TextField = new TextField();
			messageText.selectable = false;
        	messageText.autoSize = TextFieldAutoSize.LEFT;
			messageText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0x000000, true);;
			messageText.x = 40;
			messageText.y = 55;
			messageText.htmlText = message;
			addChild(messageText);
			
			var cupContainer:Sprite = new Sprite();
			var cupSprite:Sprite;
			if (weeklyRanking >= 0)
			{
				cupSprite = createCupSprite(0, weeklyRanking);
				cupContainer.addChild(cupSprite);
			}
			if (totalRanking >= 0)
			{
				cupSprite = createCupSprite(1, totalRanking);
				cupSprite.x = cupContainer.width + 20;
				cupContainer.addChild(cupSprite);
			}
			cupContainer.x = 220 - cupContainer.width / 2;
			cupContainer.y = 95;
			addChild(cupContainer);
			//计算奖励
			var awardIndex:int;
			var giftItem:GiftItem;
			oilAward = 0;
			honourAward = 0; 
			if (weeklyRanking >= 0)
			{
				awardIndex = weeklyRanking < matchConfig.oilAward.length ? weeklyRanking : matchConfig.oilAward.length - 1;
				oilAward += matchConfig.oilAward[awardIndex];
				honourAward += matchConfig.honourAward[awardIndex];
			}
			if (totalRanking >= 0)
			{
				awardIndex = totalRanking < matchConfig.oilAward.length ? totalRanking : matchConfig.oilAward.length - 1;
				oilAward += 4 * matchConfig.oilAward[awardIndex];
				honourAward += 4 * matchConfig.honourAward[awardIndex];
			}
			//分享按钮
			var ButtonBgr:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr1");
			var buttonShare:BasicButton = new BasicButton(ButtonBgr, "分享");
			buttonShare.x = (mainSprite.width  - buttonShare.width) / 4;
			buttonShare.y = mainSprite.height - buttonShare.height - 20;
			buttonShare.addEventListener(MouseEvent.CLICK, onButtonShareClick);
			addChild(buttonShare);
			
			//确定按钮
			var buttonOK:BasicButton = new BasicButton(ButtonBgr, "确定");
			buttonOK.x = (mainSprite.width  - buttonOK.width) * 3 / 4;
			buttonOK.y = mainSprite.height - buttonOK.height - 20;
			buttonOK.addEventListener(MouseEvent.CLICK, onButtonOKClick);
			addChild(buttonOK);
		}
		
		private function createCupSprite(type:int, ranking:int):Sprite
		{
			var goldCupSprite:Sprite = new Sprite();
			var GoldCup:Class = ResourceHelper.getInstance().getClassByName("GoldCup");
			var goldCup:Sprite = new GoldCup();
			goldCupSprite.addChild(goldCup);
			var colorIndex:int = Math.min(ranking, 3);
			var rankingText:TextField = new TextField();
			rankingText.selectable = false;
        	rankingText.autoSize = TextFieldAutoSize.CENTER;
			rankingText.defaultTextFormat = new TextFormat("微软雅黑", 20, 0xAAAA00, true);
			rankingText.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			rankingText.x = 40;
			rankingText.y = 65;
			var rankStr:String;
			if (type == 0)
				rankStr = "周排行: ";
			else
				rankStr = "总排行: ";
			rankingText.text = rankStr + (ranking + 1).toString() + RANK_NAME[colorIndex];
			goldCupSprite.addChild(rankingText);
			return goldCupSprite;
		}
		
		private function startFetchGift():void
		{
			var me:PlayerInfo = PlayerInfoManager.getInstance().me;
			var objectBalance:String = me.id + "," + CommonObjConfig.OIL + ",0,0,0," + oilAward
				+ "," + me.id + "," + CommonObjConfig.HONOUR + ",0,0,0," + honourAward
				+ "," + me.id + "," + CommonObjConfig.HONOUR_MEDAL + ",0,0,0," + honourAward;
			ServerHelper.getInstance().fetchGift(objectBalance);
			ServerHelper.getInstance().addEventListener(ServerEvent.FETCH_GIFT, onFetchFinish);
		}
		
		private function onFetchFinish(event:ServerEvent):void
		{
			ServerHelper.getInstance().removeEventListener(ServerEvent.FETCH_GIFT, onFetchFinish);
			var giftItems:Array = new Array();
			var giftItem:GiftItem = new GiftItem([CommonObjConfig.OIL + ",0," + oilAward]);
			giftItems.push(giftItem);
			giftItem = new GiftItem([CommonObjConfig.HONOUR + ",0," + honourAward]);
			giftItems.push(giftItem);
			var awardMessage:AwardMessage = new AwardMessage("您获得以下奖励：", "希望您再接再厉，在比赛中再创佳绩！", giftItems);
			WidgetManager.getInstance().popOut([awardMessage], true, true);
		}
		
		//点击分享
		private function onButtonShareClick(event:MouseEvent):void
		{
			ServerHelper.sendFeed("正在坦克要塞开坦克~");
			startFetchGift();
			close();
		}
		
		private function onButtonOKClick(event:MouseEvent):void
		{
			startFetchGift();
			close();
		}
		
		public function close():void
        {
        	if (this.parent != null)
        	{
        		this.parent.removeChild(this);
        	}
        }
	}
}